package com.lzx.iteam.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import com.lzx.iteam.R;

public class ContactInfoActionButtonsLayout extends LinearLayout{

   /* private List<View> mActionButtonViewList = null;
    private boolean mMirror;
    LayoutInflater mLayoutInflater;
    
    boolean mAnimating = false;
    long mLastDrawTime = -1;
    long mStartDrawTime = -1;
    
    int mAnimationDuration = 300;
    
    Interpolator mAnimationInterpolator = null;*/

    Context mContext;

    public ContactInfoActionButtonsLayout(Context context) {
        super(context);
    }

    public ContactInfoActionButtonsLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    /**
     * ViewHelper is a wrapper on the data required to animate a view
     * from a start position to a target position.
     */
   /* private class ViewHelper {
        // The start left top right bottom
        public int s_l;
        public int s_t;
        public int s_r;
        public int s_b;
        // the target (or end) left top right bottom
        public int e_l;
        public int e_t;
        public int e_r;
        public int e_b;
        public int index;
        View _v;
        public int width;
        public int height;
        public double distance;

        ViewHelper(View v, int i) {
            _v = v;
            setIndex(i);
        }

        void setStartPosition(int l, int t, int r, int b) {
            s_l = l;
            s_t = t;
            s_r = r;
            s_b = b;
        }

        void setTargetPosition(int l, int t, int r, int b) {
            e_l = l;
            e_t = t;
            e_r = r;
            e_b = b;
        }

        void setIndex(int i) {
            index = i;
        }

        void layoutAtTime(float t_req) {
            float t;

            if (mAnimationInterpolator != null) {
                t = mAnimationInterpolator.getInterpolation(t_req);
            } else {
                t = t_req;
            }
            
            int d_l = (int) ((e_l - s_l) * t);
            int cur_l = s_l + d_l;
            
            int d_t = (int) ((e_t - s_t) * t);
            int cur_t = s_t + d_t;
            
            int d_r = (int) ((e_r - s_r) * t);
            int cur_r = s_r + d_r;
            
            int d_b = (int) ((e_b - s_b) * t);
            int cur_b = s_b + d_b;
            
            _v.layout(cur_l, cur_t, cur_r, cur_b);
        }
        
        void layoutAtTimeByAcceleration(float t) {

            LogSystem.v("layout", "time: " + t); 
            double s = mV0*t + m2A*t*t;
            
            double percent = s/distance;
            LogSystem.v("layout", "percent: " + percent); 
            
            int d_l = (int) ((e_l - s_l) * percent);
            int cur_l = s_l + d_l;
            LogSystem.v("layout", "d_l: " + d_l); 
            
            int d_t = (int) ((e_t - s_t) * percent);
            int cur_t = s_t + d_t;
            LogSystem.v("layout", "d_t: " + d_t); 
            
            int d_r = (int) ((e_r - s_r) * percent);
            int cur_r = s_r + d_r;
            LogSystem.v("layout", "d_r: " + d_r); 
            
            int d_b = (int) ((e_b - s_b) * percent);
            int cur_b = s_b + d_b;
            LogSystem.v("layout", "d_b: " + d_b); 
            
            LogSystem.v("layout", "source "); 
            LogSystem.v("layout", "left:" + s_l + " top:" + s_t + " right:" + s_r + " bottom:" + s_b);
            LogSystem.v("layout", "destination "); 
            LogSystem.v("layout", "left:" + e_l + " top:" + e_t + " right:" + e_r + " bottom:" + e_b);
            LogSystem.v("layout", "current "); 
            LogSystem.v("layout", "left:" + cur_l + " top:" + cur_t + " right:" + cur_r + " bottom:" + cur_b);
            _v.layout(cur_l, cur_t, cur_r, cur_b);
        }
    }
    HashMap<View, ViewHelper> helperList = new HashMap<View, ViewHelper>();

    protected ViewHelper getViewHelper(View v, int pos) {
        ViewHelper vh = helperList.get(v);
        if (vh == null) {
            vh = new ViewHelper(v, pos);
            helperList.put(v, vh);
        }
        return vh;
    }
    
    public ContactInfoActionButtonsLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    
     * Find out if the UI is left to right
     
    public boolean isRightToLeft() {
        return mMirror;
    }

    
     * Sets or removes the focusable state of the actionbuttons.
     
    public void setActionButtonsFocusable(boolean focusable) {
        int nOfItems = mActionButtonViewList.size();
        for (int i = 0; i < nOfItems; i++) {
            mActionButtonViewList.get(i).setFocusable(focusable);
        }
    }

    public void setFocusOnFirstButton() {
        mActionButtonViewList.get(0).setFocusable(true);
        mActionButtonViewList.get(0).requestFocus();
    }


    public void animateLayout() {
        int i;

        // set up ViewHelpers to animate the views to a new location
        // by asking for new layout Rect and passing in the updated
        // selected item position.  The current position is already
        // stored away
        View v;
        ViewHelper vh;
        for (i = 0; i < getChildCount(); i++) {
            v = getChildAt(i);
            Rect rect = new Rect();
            v.getHitRect(rect);
            
            vh = getViewHelper(v,i);
            vh.setTargetPosition(rect.left,rect.top,rect.right,rect.bottom);
        }

        // once all the target positions are gathered
        // set the "please animate me" flag and call layout
        mAnimating = true;
        mStartDrawTime = -1;
        layoutChildren();
    }
    
    boolean mKeypadOfRight;
    
    private int mV0 = 5000;
    private int m2A  = 4000;
    public boolean exchangeChild() {
        
        View childView = null;
        int childCount = getChildCount();
        for (int i=0; i<childCount; i++) {
            childView = getChildAt(i);
        }
        
        ViewHelper vhFirst;
        View fistChild = getChildAt(0);
        Rect firstRect = new Rect();
        fistChild.getHitRect(firstRect);
        vhFirst = getViewHelper(fistChild, 0);
        
        ViewHelper vhSecond;
        View secondChild = getChildAt(1);
        Rect secondRect = new Rect();
        secondChild.getHitRect(secondRect);
        vhSecond = getViewHelper(secondChild, 1);
        
        if ((mKeypadAtRight && !(fistChild instanceof ContactsListView)) ||
                (!mKeypadAtRight && (fistChild instanceof ContactsListView))) {
            return false;
        }
        
        vhFirst.setTargetPosition(secondRect.width(), 0, secondRect.width() + firstRect.width(), secondRect.height());
        vhFirst.distance = mV0*mAnimationDuration + m2A*mAnimationDuration*mAnimationDuration;
        
        vhSecond.setTargetPosition(0, 0, secondRect.width(), secondRect.height());
        vhSecond.distance = mV0*mAnimationDuration + m2A*mAnimationDuration*mAnimationDuration;
        
        // once all the target positions are gathered
        // set the "please animate me" flag and call layout
        mAnimating = true;
        mStartDrawTime = -1;
        layoutChildren();
        
        return true;
    }*/
    
    @Override
    protected void dispatchDraw(Canvas canvas) {
        
      /*  if (getBackground() == null) {
            float width = (float) getWidth();
            float height = (float) getHeight();
            
            canvas.save(Canvas.MATRIX_SAVE_FLAG);
            LinearGradient fullShader = new LinearGradient(0f, 0f, 0f, 0.9f,
                    0xFF000000, 0xFF4f4f4f, Shader.TileMode.MIRROR);
            Paint fullPaint = new Paint();
            fullPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
            fullPaint.setShader(fullShader);
            
            RectF fullRect = new RectF(0.0f, 0.05f, 1.0f, .95f);
            
            canvas.scale(width, height); canvas.drawRect(fullRect, fullPaint);  
            canvas.restore();
        }*/
         
      //原来的
        View firstChild = getChildAt(2);
        Rect firstRect = new Rect();
        firstChild.getHitRect(firstRect);

        View secondChild = getChildAt(0);
        Rect secondRect = new Rect();
        secondChild.getHitRect(secondRect);
        //要改的
//        View firstChild = ((ViewGroup)getChildAt(0)).getChildAt(0);
//        Rect firstRect = new Rect();
//        firstChild.getHitRect(firstRect);
//
//        View secondChild = ((ViewGroup)getChildAt(0)).getChildAt(1);
//        Rect secondRect = new Rect();
//        secondChild.getHitRect(secondRect);



        //do not change the drawing sequence
        final long drawingTime = getDrawingTime();
        if (firstChild.getId()!= R.id.keypad_vertical) {
            if(firstChild.getVisibility()==View.VISIBLE){
                drawChild(canvas, firstChild, drawingTime);
            }
            if(secondChild.getVisibility()==View.VISIBLE){
                drawChild(canvas, secondChild, drawingTime);
            }
        } else {
            if(secondChild.getVisibility()==View.VISIBLE){
                drawChild(canvas, secondChild, drawingTime);
            }
            if(firstChild.getVisibility()==View.VISIBLE){
                drawChild(canvas, firstChild, drawingTime);
            }
        }
        
    }
  
  /*  *//**
     * To lay out the children, call the installed LayoutModel for
     * each view.  The model will return a Rect for positioning of
     * the view.  
     * 
     * Here we also handle animation between layout positions
     *//*
    private boolean mFirstTime = false;
    protected void layoutChildren() {
        

        // Is the view animating to new layout positions?
        if (mAnimating) {
            if (mStartDrawTime == -1) {
                LogSystem.v("layout", "start draw timer"); 
                LogSystem.v("layout", "mAnimationDuration:" + mAnimationDuration); 
                mStartDrawTime = SystemClock.uptimeMillis();
                mFirstTime = true;
            }
            // here we are moving the views and forcing layout
            // to update the child view locations
            // get current time
            long timenow = SystemClock.uptimeMillis();
            long diff = timenow - mStartDrawTime;
            LogSystem.v("layout", "diff:" + diff); 
            if (diff < mAnimationDuration) {
                // determine time as %
                LogSystem.v("layout", "timenow: " + timenow); 
                LogSystem.v("layout", "mStartDrawTime: " + mStartDrawTime); 
                float tdiff = (float) timenow - (float) mStartDrawTime;
                float timeslice = mStartDrawTime == -1 ? .05f : tdiff
                        / (float) mAnimationDuration;
               
                for (ViewHelper vh : helperList.values()) {
//                    if (!(vh._v instanceof ContactsListView)) {
                        vh.layoutAtTimeByAcceleration(tdiff);
//                    }
                }

                postDelayed(new Runnable() {
                    public void run() {
                        layoutChildren();
                    }
                }, 50);
            } else {
                
                LogSystem.v("layout", "animation over"); 
                mAnimating = false;
                mStartDrawTime = -1;
                mFirstTime = false;
                layoutChildren();
            }
        } else {
            
            View firstChild = getChildAt(0);
            Rect firstRect = new Rect();
            firstChild.getHitRect(firstRect);
            
            View secondChild = getChildAt(1);
            Rect secondRect = new Rect();
            secondChild.getHitRect(secondRect);
            
            this.removeAllViews();
            
            this.addView(secondChild, 0);
            this.addView(firstChild, 1);
            
            this.helperList.clear();
        }
    }
    
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        View v;
        ViewHelper vh;
        
        for (int i = 0; i < getChildCount(); i++) {
            v = getChildAt(i);
            Rect rect = new Rect();
            v.getHitRect(rect);
            vh = getViewHelper(v, i);               
            vh.setStartPosition(rect.left, rect.top, rect.right,rect.bottom);   
                      
        }
    }*/

   /* @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        PreferenceUtil preferenceUtil = PreferenceUtil.getInstance(mContext);
        boolean keypadAtRight = preferenceUtil.getBoolean(PreferenceUtil.KEYPAD_AT_RIGHT, true);
        if(keypadAtRight){
            ViewGroup keyboardRight = (ViewGroup) getChildAt(1);
            int left = getWidth() - keyboardRight.getWidth();
            int defaultTop = (getHeight()-keyboardRight.getHeight())/2;
            int top = preferenceUtil.getInt(PreferenceUtil.VERTICAL_KEYBOARD_LAST_TOP,defaultTop);
            int right = getWidth();
            int bottom = keyboardRight.getHeight()+top;
            keyboardRight.layout(left,top,right,bottom);
        }else{
            ViewGroup keyboardLeft = (ViewGroup) getChildAt(0);
            int defaultTop = (getHeight()- keyboardLeft.getHeight())/2;
            int top = preferenceUtil.getInt(PreferenceUtil.VERTICAL_KEYBOARD_LAST_TOP,defaultTop);
            int right = keyboardLeft.getWidth();
            int bottom = top+keyboardLeft.getHeight();
            keyboardLeft.layout(0,top,right,bottom);
        }
    }*/
}